function query(re) {
    if (Aid === 0) {
        return;
    }
    if (re === 0) {
        page = 1;
    }
    $.post("/ajax/getComment", {
        Aid: Aid,
        startDate: startDate,
        endDate: endDate,
        categoryId: categoryId,
        rating: rating,
        keyWord: keyWord,
        page: page,
        words: words
    }, function (json) {
        var res = $.parseJSON(json);
        totalPages = res.last_page;
        var data = res.data;
        var total = res.total;
        var tbody = $(".details-comment-table tbody");
        var length = data.length;
        var add;
        var i;
        tbody.html("");
        for (i = 0; i < length; i++) {
            if (data[i].text === "") {
                data[i].text = "无内容";
            }
            if (data[i].rating === 0) {
                data[i].rating = "无评分";
            }
            add = "<tr><td>" + ((page - 1) * 30 + i + 1) + "</td><td>" + data[i].text + "</td><td>" + data[i].rating + "</td><td>" + data[i].from_where + "</td><td>" + data[i].time + "</td></tr>";
            tbody.append(add);
        }
        $(".details-search-option-item-tips").show();
        $(".details-comment-total").text(total);
        commentPages(total);
        queryWords();
    });
}

function init() {
    Aid = 0;
    startDate = "";
    endDate = "";
    categoryId = 0;
    keyWord = new Array("");
    page = 1;
    rating = 0;
    totalPages = 0;
    words = "";
    username = $("#username").data("username");

    var start = $("#startDate");
    var end = $("#endDate");
    var date = new Date();

    date = new Date();
    endDate = dateToStr(date);
    startDate = dateToStr(date - 3600 * 1000 * 24 * 7);

    start.val(startDate);
    end.val(endDate);
}

function appChange() {
    $("#details-app").change(function () {
        Aid = $($(this)[0].selectedOptions).data("appid");
        query(0);
    });
}

function datepicker() {
    var datepicker = $(".datepicker");
    var dateinput = $(".date");
    var start = $("#startDate");
    var end = $("#endDate");

    var date = new Date();

    datepicker.datepicker({
        changeMonth: true,
        changeYear: true,
        minDate: "-2Y",
        maxDate: 0,
        dateFormat: "yy-mm-dd"
    });

    datepicker.change(function () {
        $(".datepicker-a").removeClass("actived");
        datepicker.eq(0).datepicker("option", "maxDate", $("#endDate").val());
        startDate = start.val();
        endDate = end.val();
        query(0);
    });

    dateinput.delegate(".datepicker-a", "click", function () {
        switch ($(this).text()) {
            case "最近一周": {
                date = new Date();
                startDate = dateToStr(date - 3600 * 1000 * 24 * 7);
                endDate = dateToStr(date);
                break;
            }
            case "最近一个月": {
                date = new Date();
                endDate = dateToStr(date);
                date.setMonth(date.getMonth() - 1);
                startDate = dateToStr(date);
                break;
            }
            case "最近三个月": {
                date = new Date();
                endDate = dateToStr(date);
                date.setMonth(date.getMonth() - 3);
                startDate = dateToStr(date);
                break;
            }
            case "最近一年": {
                date = new Date();
                endDate = dateToStr(date);
                date.setMonth(date.getMonth() - 12);
                startDate = dateToStr(date);
                break;
            }
        }
        start.val(startDate);
        start.change();
        end.val(endDate);
        $(this).addClass("actived");
    });
}

function categoryChange() {
    $(".category").delegate("a", "click", function () {
        $(".category a").removeClass("actived");
        $(this).addClass("actived");
        categoryId = $(this).data("categoryid");
        query(0);
    });
}

function ratingChange() {
    $(".rating").delegate("a", "click", function () {
        $(".rating a").removeClass("actived");
        $(this).addClass("actived");
        rating = $(this).data("categoryid");
        query(0);
    });
}

function commentPages(total) {
    var pageGroup = $(".details-comment-pages-group");
    var start;
    var end;
    pageGroup.html("");
    pageGroup.append('<btn class="glyphicon glyphicon-menu-left details-comment-pages-page details-comment-pages-pre"></btn>');
    if (totalPages > 10) {
        if (page < 5) {
            start = 0;
        } else if (totalPages - page < 5) {
            start = totalPages - 10;
        } else {
            start = page - 5;
        }
        end = start + 10;

        for (; start < end; start++) {
            pageGroup.append('<btn class="details-comment-pages-page">' + (start + 1) + '</btn>');
        }
    } else {
        for (start = 0; start < totalPages; start++) {
            pageGroup.append('<btn class="details-comment-pages-page">' + (start + 1) + '</btn>');
        }
    }
    pageGroup.append('<btn class="glyphicon glyphicon-menu-right details-comment-pages-page details-comment-pages-next"></btn>');

    pageGroup.children().each(function () {
        if (parseInt($(this).text()) === page) {
            $(this).addClass("actived");
        }
    });
}

function commnetPageBind() {
    var pageGroup = $(".details-comment-pages-group");
    pageGroup.delegate("btn", "click", function () {
        var btn = $(this);
        if (btn.hasClass("actived")) {
            return;
        }

        var text = btn.text();
        if (btn.hasClass("details-comment-pages-pre")) {
            if (page !== 1) {
                page--;
            } else {
                return;
            }
        } else if (btn.hasClass("details-comment-pages-next")) {
            if (page !== totalPages) {
                page++;
            } else {
                return;
            }
        } else {
            page = parseInt(text);
        }
        pageGroup.children().removeClass("actived");
        query(1);
    });
}

function queryWords() {
    $.post("/ajax/getHotWords", {
        Aid: Aid,
        username: username,
        startDate: startDate,
        endDate: endDate,
        categoryId: categoryId,
        rating: rating
    }, function (json) {
        var res = $.parseJSON(json);
        var hot = res.hot.data;
        var my = res.user.data;
        var hotTotal = res.hot.total;
        var myTotal = res.user.total;
        if (hotTotal > 10) {
            hotTotal = 10;
        }
        if (myTotal > 10) {
            myTotal = 10;
        }
        var i;
        var hotTableBody = $(".details-words-hot-table tbody");
        var myTableBody = $(".details-words-my-table tbody");

        hotTableBody.html("");
        for (i = 0; i < hotTotal; i++) {
            hotTableBody.append("<tr><td>" + (i + 1) + "</td><td>" + hot.text[i] + "</td><td>" + hot.weight[i] + "</td></tr>");
        }
        myTableBody.html("");
        for (i = 0; i < myTotal; i++) {
            myTableBody.append("<tr><td>" + (i + 1) + "</td><td>" + my.text[i] + "</td><td>" + my.weight[i] + "</td></tr>");
        }
    });
}

function search() {
    var searchInput = $(".details-search-option-item-search-input");
    var text = "";
    var wordsDiv = $(".details-search-option-item-search-words");
    var word = "";
    $(".details-search-option-item-search-btn").click(function () {
        wordsDiv.html("");
        text = searchInput.val();
        words = text.split(" ", 5);
        words = removeDuplicatedItem(words);
        words.forEach(function (v, index, arr) {
            word = $('<span class="capsule"></span>');
            word.text(v);
            wordsDiv.append(word);
            query(0);
        });
    });
}

function removeDuplicatedItem(ar) {
    var ret = [];

    for (var i = 0, j = ar.length; i < j; i++) {
        if (ret.indexOf(ar[i]) === -1 && ar[i].replace(" ","") !== "") {
            ret.push(ar[i]);
        }
    }

    return ret;
}

$(function () {
    init();
    appChange();
    datepicker();
    categoryChange();
    ratingChange();
    commnetPageBind();
    search();
});